#property indicator_separate_window
//----
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Gold
#property indicator_color4 Gold
#property indicator_color5 Black
#property indicator_color6 Red
#property indicator_color7 DodgerBlue
#property indicator_color8 Black

extern int Lead = 8;
int g_ma_method_80 = MODE_EMA;
int g_applied_price_84 = PRICE_CLOSE;
extern int Follow = 13;
int g_ma_method_92 = MODE_EMA;
int g_applied_price_96 = PRICE_CLOSE;
int g_period_100 = 13;
int g_ma_method_104 = MODE_SMA;
double gd_108 = 50.0;
double buffer7[];
double buffer8[];
double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer6[];
double buffer5[];
double gd_148 = 0.0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   g_period_100 = Follow;
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS) + 1.0);
   SetIndexStyle(6, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(6, buffer7);
   SetIndexDrawBegin(6, Follow);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(5, buffer6);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(0, buffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(1, buffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexBuffer(2, buffer3);
   SetIndexStyle(3, DRAW_HISTOGRAM);
   SetIndexBuffer(3, buffer4);
   SetIndexStyle(7, DRAW_NONE);
   SetIndexBuffer(7, buffer8);
   SetIndexStyle(4, DRAW_HISTOGRAM, STYLE_SOLID);
   SetIndexBuffer(4, buffer5);
   IndicatorShortName("DPI");
   SetIndexLabel(0, "Lead");
   SetIndexLabel(1, "Follow");
   g_period_100 = Follow;
   gd_148 = gd_108 / 100.0;
//----
   return(0);
  }
//+------------------------------------------------------------------+

int start()
{
   double ld_4;
   double ld_12;
   double ld_20;
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for (int i = 0; i < limit; i++) buffer7[i] = iMA(NULL, 0, Lead, 0, g_ma_method_80, g_applied_price_84, i) - iMA(NULL, 0, Follow, 0, g_ma_method_92, g_applied_price_96, i);
   for (i = 0; i < limit; i++) buffer8[i] = iMAOnArray(buffer7, Bars, g_period_100, 0, g_ma_method_104, i);
    
   for (i = 0; i < limit; i++) {
      ld_4 = buffer7[i] - buffer8[i];
      buffer6[i] = ld_4;
      buffer5[i] = EMPTY_VALUE;
      
      if (buffer6[i] > 0.0 && buffer7[i] > 0.0) buffer5[i] = MathMin(buffer6[i], buffer7[i]);
      
      
      if (buffer6[i] < 0.0 && buffer7[i] < 0.0) 
      {
          buffer5[i] = MathMax(buffer6[i], buffer7[i]);
      }
      
      if (buffer7[i] > 0.0) ld_12 = MathMax(buffer6[i], buffer7[i]);
      else ld_12 = 0;
      
     
      if (buffer6[i] < 0.0) ld_20 = MathMin(buffer6[i], buffer7[i]);
      else {
         ld_20 = 0;
         ld_12 = MathMax(buffer6[i], buffer7[i]);
      }
                        
      if (buffer7[i] < 0.0) ld_20 = MathMin(buffer6[i], buffer7[i]);
      
      if (MathAbs(ld_12) >= MathAbs(ld_20)) 
      {
         buffer3[i] = MathMax(ld_12, ld_20);
         if (ld_12 < 0.0 || ld_20 < 0.0) buffer4[i] = MathMin(ld_12, ld_20);
         else buffer4[i] = EMPTY_VALUE;
         buffer1[i] = EMPTY_VALUE;
         buffer2[i] = EMPTY_VALUE;
      } else {
         buffer1[i] = MathMin(ld_12, ld_20);
         if (ld_12 > 0.0 || ld_20 > 0.0) buffer2[i] = MathMax(ld_12, ld_20);
         else buffer2[i] = EMPTY_VALUE;
         buffer3[i] = EMPTY_VALUE;
         buffer4[i] = EMPTY_VALUE;
      }
      
   }

   return (0);
}
//+------------------------------------------------------------------+

